﻿using AntdUI;

namespace WenAntdUI.Controls;

[ToolboxItem(true)]
public class Imgbox : IControl
{
    private Image? image;

    public Imgbox() : base() { }

    [Category("Wen"), Description("图档")]
    public Image? Image
    {
        get => image;
        set
        {
            if (image != value)
            {
                image = value;
                base.OnPropertyChanged(nameof(Image));
            }
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (image != null)
        {
            var g = e.Graphics.High();

            //保持比例缩放
            var aspectRatio = (float)image.Width / image.Height;
            var newWidth = Width;
            var newHeight = (int)(Width / aspectRatio);
            if (newHeight > Height)
            {
                newHeight = Height;
                newWidth = (int)(Height * aspectRatio);
            }
            var x = (Width - newWidth) / 2;
            var y = (Height - newHeight) / 2;
            var rect = new Rectangle(x, y, newWidth, newHeight).PaddingRect(this.Padding);

            g.Image(image, rect);
        }
    }
}